import { CoolLocalStorage } from 'src/app/common/coolStorage/cool-local-storage';
import { Toast } from 'ng-zorro-antd-mobile';
import { group } from '@angular/animations';
import { DetailService } from './../estate-detail/services/detail.service';
import { ActivatedRoute } from '@angular/router';
import { Component, OnInit } from '@angular/core'
import { Location } from '@angular/common'
import { Title } from '@angular/platform-browser'

@Component({
    selector: 'app-estate-info',
    templateUrl: './estate-info.component.html',
    styleUrls: ['./estate-info.component.scss']
})
export class EstateInfoComponent implements OnInit {

    public list: any = [];
    public loading:boolean=true;

    title = ''

    constructor(
        private location: Location,
        private titleService: Title,
        private routeInfo: ActivatedRoute,
        private detailService: DetailService,
        private coolLocalStorage: CoolLocalStorage,
        private _toast: Toast

    ) { }

    ngOnInit() {

        let id = this.routeInfo.snapshot.queryParams['project'];

        this.getEstateDetail(id)

    }

    onClickLeft() {
        this.location.back()
    }

    getEstateDetail(id) {

        let reqdata = {
            project: id,
            zone:this.coolLocalStorage.getItem('zone')
        }

        this.detailService.getEstateDetail(reqdata, res => {

            if (res.Success && res.Result) {

                let items = res.Result;
                let Description = this._jp(items.Description)

                //设置title
                this.title = items.Name;
                this.titleService.setTitle(items.Name)

                //基本信息
                this.base(items);
                //描述
                this.description(Description);
            }

            this.loading = false;

        })
    }


    base(items) {
        let o = {
            Group: items.Name,
            Tag: this._jp(items.Tag),
            Child: [
                { Name: '楼盘状态', Value: items.Status, },
                { Name: '参考均价', Value: items.AveragePrice, },
                { Name: '开盘时间', Value: items.OpenDay, },
                { Name: '交房时间', Value: items.CloseDay, },
                { Name: '楼盘地址', Value: items.Address, },
            ]
        }

        this.list.push(o);
    }
    description(des) {
        this.formatList(des).forEach(element => {
            this.list.push(element);
        });
    }

    private formatList(des) {
        let o = [];

        des.forEach(element => {
            let _o = {
                Group: '',
                Child: [],
            }
            _o['Group'] = element['Group']
            _o['Child'].push({
                'Name': element['Name'],
                'Value': element['Value']
            });
            o.push(_o);
        });


        let nO = [];
        for (let i = 0; i < o.length; i++) {

            let _p: any = {};
            let element = o[i];

            if (i != 0) {
                _p = o[i - 1];
            }

            if (element.Group != _p['Group']) {
                nO.push({ "Group": element.Group, Child: [] });
            }

        }

        for (let k = 0; k < o.length; k++) {
            let _o = o[k]
            nO.forEach(element => {
                if (_o.Group === element.Group) {
                    element.Child.push(_o.Child[0]);
                }
            });
        }
        return nO;
    }

    private _jp(str) {
        if(typeof(str) ==='string'){
            return JSON.parse(str)
        }else{
            return str;
        }
    }
}
